package com.ljl.util;

import org.springframework.util.Base64Utils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @author 刘家良
 * @date 2022/10/30 19:51
 * @description /
 */
public class ImageTools {

    public static void saveBufferedImage(BufferedImage bufferedImage, String outputFile) throws IOException {
        FileOutputStream out = new FileOutputStream(outputFile);
        ImageIO.write(bufferedImage, "jpg", out);
        out.close();
    }

    public static BufferedImage getBufferedImage(String imgPath) throws IOException {
        BufferedImage image = ImageIO.read(new File(imgPath));
        BufferedImage resizedImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
        Graphics2D g = resizedImage.createGraphics();
        g.drawImage(image, 0, 0, null);
        g.dispose();
        return resizedImage;
    }

    public static String imgFileToBase64(String imgPath) {
        byte[] imgBytes = getImgBytes(imgPath);
        return Base64Utils.encodeToString(imgBytes);
    }

    /**
     * base64转图片存储在本地
     * @param imgBase64 图片base64
     * @param imgPath 图片本地存储地址
     */
    public static boolean base64ToImgFile(String imgBase64, String imgPath) {
        boolean flag = true;
        try (OutputStream outputStream = new FileOutputStream(imgPath);){
            // 解密处理数据
            byte[] bytes = Base64Utils.decodeFromString(imgBase64);
            for (int i = 0; i < bytes.length; ++i) {
                if (bytes[i] < 0) {
                    bytes[i] += 256;
                }
            }
            outputStream.write(bytes);
        } catch (Exception e) {
            e.printStackTrace();
            flag = false;
        }
        return flag;
    }

    public static byte[] getImgBytes(String imgPath) {
        InputStream inputStream = null;
        byte[] buffer = null;
        try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream();){
            //判断网络链接图片文件/本地目录图片文件
            if (imgPath.startsWith("http://") || imgPath.startsWith("https://")) {
                // 创建URL
                URL url = new URL(imgPath);
                // 创建链接
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("GET");
                conn.setConnectTimeout(5000);
                inputStream = conn.getInputStream();
                // 将内容读取内存中
                buffer = new byte[1024];
                int len = -1;
                while ((len = inputStream.read(buffer)) != -1) {
                    outputStream.write(buffer, 0, len);
                }
                buffer = outputStream.toByteArray();
            } else {
                inputStream = new FileInputStream(imgPath);
                int count = 0;
                while (count == 0) {
                    count = inputStream.available();
                }
                buffer = new byte[count];
                inputStream.read(buffer);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    // 关闭inputStream流
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return buffer;
    }

}
